package com.qf.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.qf.config.RedisKey;
import com.qf.dao.ChildParentingRepository;
import com.qf.pojo.respVo.BaseResp;
import com.qf.pojo.vo.entity.ChildParenting;
import com.qf.service.ChildParentingService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName ChildParentingServiceImpl
 * @Description TODO
 * @Author Nslsg
 * @Date 2022/11/4 16:50
 * @Version 1.0
 **/
@Service
public class ChildParentingServiceImpl implements ChildParentingService {

    @Autowired
    ChildParentingRepository repository;

    @Autowired
    RedisTemplate redisTemplate;

    @Override
    public BaseResp findAll() {
        List<ChildParenting> all = repository.findAll();
        return new BaseResp().OK("查询成功!", all, null);
    }

    @Override
    public BaseResp findByType(Integer type, Integer page, Integer size) {
        List range = redisTemplate.boundListOps(RedisKey.PATERNITY_LIST + type.toString()).range(0, -1);

        if (range == null || range.size() == 0) {
            List<ChildParenting> byType = repository.findByType(type);
            if (type <= 0 || type > 3) {
                if (byType == null || byType.size() == 0) {
                    redisTemplate.opsForValue().set(RedisKey.PATERNITY_LIST + type.toString(), "没有该类数据!", 30, TimeUnit.SECONDS);
                }
                return new BaseResp().Fail(0, "查询失败,没有该条数据!");

            }
            redisTemplate.opsForList().rightPushAll(RedisKey.PATERNITY_LIST + type.toString(), byType);
        }

        Integer start = (page - 1) * size;
        Integer end = start + size - 1;
        List courseList = redisTemplate.boundListOps(RedisKey.PATERNITY_LIST + type.toString()).range(start, end);
        Long total = redisTemplate.boundListOps(RedisKey.PATERNITY_LIST + type.toString()).size();

        return new BaseResp().OK("查询成功", courseList, total);
    }

    @Override
    public BaseResp findByAllId(Integer allId, Integer page, Integer size) {
        List range = redisTemplate.boundListOps(RedisKey.PATERNITY_ALLID + allId.toString()).range(0, -1);
        if (range == null || range.size() == 0) {
            List<ChildParenting> byAllId = repository.findByAllId(allId);
            if (allId <= 0 || allId > 12) {
                if (byAllId == null || byAllId.size() == 0) {
                    redisTemplate.opsForValue().set(RedisKey.PATERNITY_ALLID + allId.toString(), "没有该类数据!", 30, TimeUnit.SECONDS);
                }
                return new BaseResp().Fail(0, "查询失败,没有该条数据!");

            }
            redisTemplate.opsForList().rightPushAll(RedisKey.PATERNITY_ALLID + allId.toString(), byAllId);
        }

        Integer start = (page - 1) * size;
        Integer end = start + size - 1;
        List courseList = redisTemplate.boundListOps(RedisKey.PATERNITY_ALLID + allId.toString()).range(start, end);
        Long total = redisTemplate.boundListOps(RedisKey.PATERNITY_ALLID + allId.toString()).size();

        return new BaseResp().OK("查询成功", courseList, total);
    }

    @Override
    public BaseResp findById(Integer id) {
        List range = redisTemplate.boundListOps(RedisKey.PATERNITY_ID+id.toString()).range(0, -1);
        if (range==null ||range.size()==0){
            ChildParenting byId = repository.findById(id).get();
            List list = new ArrayList();
            list.add(byId);
            if (id<=0 || id>list.size()){
                if (list.size()==0){
                    redisTemplate.opsForValue().set(RedisKey.PATERNITY_ID + id.toString(), "没有该类数据!", 30, TimeUnit.SECONDS);
                }
                return new BaseResp().Fail(0, "查询失败,没有该条数据!");
            }
            redisTemplate.opsForList().rightPushAll(RedisKey.PATERNITY_ID + id.toString(), list);

        }
        return new BaseResp().OK("查询成功", range, null);
    }
}